{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <div class="layui-form toolbar">

                <div class="layui-form-item">

                    <div class="layui-input-inline">
                        <select name="position_id" lay-verify="" id="position_id">
                            <option value="">请选择广告位置</option>
                            {foreach $data as $v}
                            <option value="{$v.id}">{$v.name}</option>
                            {/foreach}
                        </select>
                    </div>

                    <div class="layui-inline">
                        <div class="layui-input-inline mr0">
                            <input name="title" class="layui-input" type="text" placeholder="请输入标题"/>
                        </div>
                    </div>

                    <div class="layui-inline" style="padding-right: 110px;">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchUser" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAddUser" class="layui-btn icon-btn {:auth('banner.advert_msg/add')}" ><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>

                </div>
            </div>

            <table id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarUser">
    <a class="layui-btn layui-btn-sm layui-btn-xs {:auth('banner.advert_msg/edit')}" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs {:auth('banner.advert_msg/delete')}" lay-event="del">删除</a>
</script>


<div class="layui-card-body">
    <script type="text/html" id="thumb">

        {{# if(d.thumb){ }}
        <a href="{{d.thumb}}" target="_blank" title="点击查看">
            <img src="{{d.thumb}}" alt="" width="28" height="28">
        </a>
        {{#  } }}
    </script>
</div>
<script type="text/html" id="advert_name">
    {{d.user.name }}
</script>
<!-- js部分 -->
{include file='public/footer' /}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: '{:__url("banner.advert_msg/getData")}',
            page: true,
            toolbar: true,
            cellMinWidth: 100,
            cols: [[ //表头
                {checkbox: true,fixed: true}
                ,{field: 'advert_name', title: '广告位置',toolbar:'#advert_name'}
                ,{field: 'title', title: '广告位标题'}
                ,{field: 'thumb', title: '图片',toolbar:'#thumb'}
                ,{field: 'create_time', title: '创建时间'}
                ,{field: 'update_time', title: '修改时间'}
                
                , {fixed: 'right', width: 140, align: 'center', toolbar: '#tableBarUser'}
            ]]
        });

        // 添加
        $('#btnAddUser').click(function () {
            showEditModel();
        });

        // 搜索
        form.on('submit(formSubSearchUser)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;

            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            }else if (layEvent === 'add') { // 删除
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.title);
            } else if (layEvent === 'reset') { // 重置密码
                resetPsw(data.userId, data.username);
            }
        });

        // 显示表单弹窗
        function showEditModel(mUser) {
            layer.full(admin.open({
                type: 2,
                title: (mUser ? '修改' : '添加') + '广告信息',
                content: (mUser ? "edit?id="+mUser.id : "add"),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url = mUser ? '{:__url("banner.advert_msg/edit")}' : '{:__url("banner.advert_msg/add")}';
                    //mUser &&  mUser.id;

                    // 回显数据
                    form.val('modelUserForm', mUser);
                    // 表单提交事件
                    form.on('submit(modelSubmitUser)', function (data) {
                        layer.load(2);
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 1) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            }));
        }

        // 删除
        function doDel(userId, nickName) {

            layer.confirm('确定要删除“' + nickName + '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.get('{:__url("banner.advert_msg/delete")}', {
                    id: userId
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 1) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

    });
</script>

</body>
</html>
